package com.whjfxx.terminal.websocket;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Scanner;

/**
 * Created by b.c on 2022/1/18 9:42
 */
public class Test {

    public static void main(String[] args) {
        try {
            WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:6060/websocket/test")) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    System.out.println( "opened connection" );
                }

                @Override
                public void onMessage(String message) {
                    System.out.println(message);
                }

                @Override
                public void onClose(int i, String s, boolean remote) {
                    System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) );
                }

                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                }
            };

            Scanner input=new Scanner(System.in);

            webSocketClient.connect();
            WebSocket webSocket = webSocketClient.getConnection();
            while (true) {
                String str = input.next();
                webSocket.send(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
